const fs = require("fs");
const { getTime } = require("./data");
const pathPrefix = __dirname + "/log/";

let lastTime = new Date().getTime();
const createNewInterval = 1 * 24 * 3600 * 1000; //1天
// const createNewInterval = 10000;

function log(...args) {
    console.log(...args);
    let tmp = "";
    for (let i of args) {
        if (tmp) {
            tmp += " ";
        }
        let str = "";
        const type = typeof i;

        if (type == "object" || i === void 0 || i === null) {
            str = JSON.stringify(i);
        } else if (type == "string") {
            str = i;
        } else {
            str = i.toString();
        }
        tmp += str;
    }
    tmp += "\n";

    let path;
    const curTime = new Date().getTime();
    if (curTime > lastTime + createNewInterval) {
        lastTime = curTime;
    }
    path = lastTime + ".log";
    fs.writeFileSync(pathPrefix + path, `[${getTime()}] ${tmp}`, { flag: "a" });
}

module.exports = {
    fileLog: log
}